const jwt = require('jsonwebtoken');
const users = require('jane-users');
class team extends users {

	/**
	 * 发送邀请
	 */
    async add() {
        try {
            let get_user = await sql.name('users').where({ username: $_POST.get_invite }).first();
            if (get_user) {
                if (get_user.uuid) {
                    throw new Exception('用户已加入其它团队', 10001);
                }
            } else {
                throw new Exception('用户不存在', 10001);
            }

            let token = $_SERVER.authorization;
            let token_info = jwt.verify(token, 'qinuoyun', function(err, res) {
                return res;
            })

            let send_user = await sql.name('users').where({ _id: token_info.id }).first();
            if (send_user.status == 1 && send_user.uuid) {
                let data = { send_invite: send_user.username, get_invite: get_user.username, invite_uuid: send_user.uuid, invite_status: 1 };
                let check = await sql.name('invite').where(data).first();
                if (check) {
                    throw new Exception('请勿重复邀请', 10001);
                } else {

                    return await sql.name('invite').insert(data);
                }
            } else {
                throw new Exception('你无邀请权限', 10001);
            }
        } catch (error) {
            throw new Exception(error);
        }

    }

    /**
     * 团队里的人
     * @return {[type]} [description]
     */
    async invite_list() {
        try {
            let token = $_SERVER.authorization;
            let token_info = jwt.verify(token, 'qinuoyun', function(err, res) {
                return res;
            })

            let user = await sql.name('users').where({ _id: token_info.id }).first();
            return await sql.name('users').where({ uuid: user.uuid }).select();
        } catch (error) {
            throw new Exception(error);
        }
    }

    /**
     * 我收到的邀请
     * @return {[type]} [description]
     */
    async my_invite() {
        try {
            let token = $_SERVER.authorization;
            let token_info = jwt.verify(token, 'qinuoyun', function(err, res) {
                return res;
            })

            let user = await sql.name('users').where({ _id: token_info.id }).first();
            if (user.uuid) {
                return false;
            } else {
                return await sql.name('invite').where({ get_invite: user.username, invite_status: 1 }).select();
            }
        } catch (error) {
            throw new Exception(error);
        }
    }

    /**
     * 加入团队
     * @return {[type]} [description]
     */
    async join_team() {
        try {
            let token = $_SERVER.authorization;
            let token_info = jwt.verify(token, 'qinuoyun', function(err, res) {
                return res;
            })

            let info = await sql.name('invite').where({ _id: $_POST._id }).first();

            if (info.get_invite == token_info.username) {
                sql.name('invite').where({ _id: $_POST._id }).update({ invite_status: 2 });
                return sql.name('users').where({ _id: token_info.id }).update({ roles: 2, uuid: info.invite_uuid })
            } else {
                return false;
            }

        } catch (error) {
            throw new Exception(error);
        }
    }

    /**
     * 请离团队
     * @return {[type]} [description]
     */
    async leave_team() {
        try {
            let token = $_SERVER.authorization;
            let token_info = jwt.verify(token, 'qinuoyun', function(err, res) {
                return res;
            })


            let user = await sql.name('users').where({ _id: token_info.id }).first();

            if (user.roles == 1) {
                if ($_POST._id == user._id) {
                    throw new Exception('你不能脱离团队', 10001);
                } else {
                    return await sql.name('users').where({ _id: $_POST._id, uuid: user.uuid }).update({ roles: 1, uuid: '' });
                }
            } else {
                throw new Exception('你没有该权限', 10001);
            }

        } catch (error) {
            throw new Exception(error);
        }
    }
}

module.exports = team;